package com.loadmore.common.auth

import android.os.Bundle
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.loadmore.common.R
import com.loadmore.common.base.BaseActivity
import com.loadmore.common.constants.Api
import com.loadmore.common.model.BaseResponse
import com.loadmore.common.model.User
import com.loadmore.common.net.Request
import com.loadmore.common.net.impl.JsonCallback
import com.loadmore.common.router.RouterPath
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_register.*
import java.util.*

@Route(path = RouterPath.Register)
class RegisterActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        register.setOnClickListener {
            var params = HashMap<String,Any>()
            params["name"] = name.text.toString()
            params["email"] = email.text.toString()
            params["password"] = password.text.toString()
            Request.post(Api.register,params,object :JsonCallback<BaseResponse<User>>(){
                override fun onSuccess(response: Response<BaseResponse<User>>?) {
                    var result = response?.body()
                    if(result?.code == 200) {
                        Toast.makeText(applicationContext,"注册成功!",Toast.LENGTH_SHORT)
                        ARouter.getInstance().build(RouterPath.Login).navigation()
                        finish()
                    }else{
                        Toast.makeText(applicationContext,"注册失败!",Toast.LENGTH_SHORT)
                    }
                }

                override fun onError(response: Response<BaseResponse<User>>?) {
                    super.onError(response)
                    Toast.makeText(applicationContext,"注册失败!",Toast.LENGTH_SHORT)
                }

            })
        }
    }
}
